package com.zhao.shiyuanbaoyou.activity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zhao.shiyuanbaoyou.R;
import com.zhao.shiyuanbaoyou.fragment.BrandFragment;
import com.zhao.shiyuanbaoyou.fragment.HomeFragment;
import com.zhao.shiyuanbaoyou.fragment.MyCenter;
import com.zhao.shiyuanbaoyou.fragment.TenFragment;
import com.zhao.shiyuanbaoyou.fragment.WorthFragment;
import com.zhao.shiyuanbaoyou.helper.FragmentHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup_main;
    private Context mContext=this;
    private FrameLayout frameLayout_main;
    //底部导航按钮
    private RadioButton[] mRadioButton;
    //底部导航数组
    private String[] mArrTabTitle = null;
    //底部导航图片资源
    private Drawable[] mArrDrawable = null;
    //碎片集合
    private List<Fragment> mList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        iniDataDrawn();
        initTable();
    }

    private void iniDataDrawn() {
        //底部标题数组
        mArrTabTitle = getResources().getStringArray(R.array.arrTabTitles);
        //底部导航图片数组
        TypedArray array = getResources().obtainTypedArray(R.array.arrIconDrawable);
        //赋值给drawable对象
        mArrDrawable = new Drawable[array.length()];
        for (int i = 0; i < mArrDrawable.length; i++) {
            mArrDrawable[i] = array.getDrawable(i);
        }
        HomeFragment homeFragment=new HomeFragment();
        mList.add(homeFragment);
        BrandFragment brandFragment=new BrandFragment();
        mList.add(brandFragment);
        TenFragment tenFragment=new TenFragment();
        mList.add(tenFragment);
        WorthFragment worthFragment=new WorthFragment();
        mList.add(worthFragment);
        MyCenter myCenter=new MyCenter();
        mList.add(myCenter);
        //设置首页碎片，如果不设置，打开软件，当点击时候才会生成碎片布局
        FragmentHelper.switchFragment(getSupportFragmentManager(),mList,0,R.id.FrameLayout_main_container,0,0);
    }

    private void initView() {
        frameLayout_main = (FrameLayout) findViewById(R.id.FrameLayout_main_container);
        radioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
    }

    private void initTable() {
        //设置底部导航初始和点击后的颜色
        final int otherColor = getResources().getColor(R.color.deeppink);
        final int selectColor = getResources().getColor(R.color.lightgrey);
        //获取屏幕宽度
        int widthPixels = getResources().getDisplayMetrics().widthPixels;
        //初始化按钮
        mRadioButton =new RadioButton[mArrTabTitle.length];
        for (int i = 0; i < mArrTabTitle.length; i++) {
            //动态生成按钮
            mRadioButton[i]=new RadioButton(mContext);
            //设置按钮底部文字
            mRadioButton[i].setText(mArrTabTitle[i]);
            //设置底部导航按钮颜色
            mRadioButton[i].setButtonDrawable(android.R.color.transparent);
            //让文字居中
            mRadioButton[i].setGravity(Gravity.CENTER);
            //取消点击单选按钮产生的背景
            mRadioButton[i].setBackgroundResource(android.R.color.transparent);
            //给按钮占一个位置放置图标，设置四个边界
            mArrDrawable[i].setBounds(0,0,mArrDrawable[i].getIntrinsicWidth(), mArrDrawable[i].getIntrinsicHeight());
            //设置按钮上的图标
            mRadioButton[i].setCompoundDrawables(null, mArrDrawable[i], null, null);
            //设置默认文字颜色
            mRadioButton[i].setTextColor(selectColor);
            //设置图标默认颜色
            mArrDrawable[i].setColorFilter(selectColor, PorterDuff.Mode.SRC_IN);
            //设置按钮的宽度
            mRadioButton[i].setWidth(widthPixels/mArrTabTitle.length);
            //将生产的按钮加入到管理组
            radioGroup_main.addView(mRadioButton[i]);

        }
        //设置默认按钮文字的颜色
        mRadioButton[0].setTextColor(otherColor);
        //默认按钮图标颜色
        mArrDrawable[0].setColorFilter(otherColor,PorterDuff.Mode.SRC_IN);
        //设置监听
        radioGroup_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < mArrTabTitle.length; i++) {
                    //设置默认颜色
                    mRadioButton[i].setTextColor(selectColor);
                    mArrDrawable[i].setColorFilter(selectColor,PorterDuff.Mode.SRC_IN);
                    //设置点击底部导航后图片切换的颜色
                    if (mRadioButton[i].getId()==checkedId){
                        mRadioButton[i].setTextColor(otherColor);
                        mArrDrawable[i].setColorFilter(otherColor,PorterDuff.Mode.SRC_IN);
                        //动态生成碎片
                      FragmentHelper.switchFragment(getSupportFragmentManager(),mList,i,R.id.FrameLayout_main_container,0,0);
                    }
                }
            }
        });
    }
}
